<?php
	error_reporting('E_ERROR');	
	include_once "../inc_function.php";     
//	include_once '../../function/class_form.php';

    global $con;
    
    $action = isset($_REQUEST['form']) && $_REQUEST['form'] == 'update' && !empty($_REQUEST['update'])?'update':'add';    
    
    $pic = isset($_SESSION['employee_id']) && $_SESSION['employee_id'] != 0?$_SESSION['employee_id']:0;
	$desc = (isset($_POST['txt_description']) && !empty($_POST['txt_description']))?$_POST['txt_description']:'';
	$start_date = (isset($_POST['txt_start_date']) && !empty($_POST['txt_start_date']))?$_POST['txt_start_date']:'';
	$end_date = (isset($_POST['txt_end_date']) && !empty($_POST['txt_end_date']))?$_POST['txt_end_date']:'';

	$data = array(
		'id' => $_REQUEST['update'],
    	'pic' => $pic,
		'desc' => $desc,
		'start_date' => $start_date,
		'end_date' => $end_date,
		'chkCorp' => $_POST['chkCorp']	 
    );
    
	if(isset($_REQUEST['form']) && $_REQUEST['form'] == 'update' && !empty($_REQUEST['update'])){
		$curs = oci_new_cursor($con);
		$stmt2 = oci_parse($con,"begin PKG_ANNOUNCEMENT.GET_ANNOUNCEMENT_ID(:id,:data);end;");
		oci_bind_by_name($stmt2,":id",$_REQUEST['update']);		
		oci_bind_by_name($stmt2,":data",$curs,-1,OCI_B_CURSOR);
		oci_execute($stmt2,OCI_DEFAULT);
		oci_execute($curs,OCI_DEFAULT);
		oci_free_statement($stmt2);

		while (OCIFetchInto($curs,$curs_list)){
			$id = $curs_list[0];
			$desc = $curs_list[1];
			$create_date = $curs_list[2];
			$create_id = $curs_list[3];
			$update_date = $curs_list[4];
			$update_id = $curs_list[5];
			$start_date = date('m/d/Y',strtotime($curs_list[6]));
			$end_date = date('m/d/Y',strtotime($curs_list[7]));
			$company_id = $curs_list[8];
//			echo $company_code = $curs_list[9];
//			echo $company_name = $curs_list[10];
		}		
		$company = explode(',',$company_id);
	}

	
	
	function insertData($action,$con,$data,$POST){
		switch($action){
			case 'add':
				$stmt = oci_parse($con,'BEGIN PKG_ANNOUNCEMENT.INSERT_ANNOUNCEMENT(:pic,:description,:start_date,:end_date,:thisID);END;');
				oci_bind_by_name($stmt,':pic',$data['pic']);
				oci_bind_by_name($stmt,':description',$data['desc']);
				oci_bind_by_name($stmt,':start_date',$data['start_date']);
				oci_bind_by_name($stmt,':end_date',$data['end_date']);
				oci_bind_by_name($stmt,':thisID',$announcement_id,10);
				oci_execute($stmt,OCI_DEFAULT);
	         	oci_free_statement($stmt);         			
				
				bodyProcedure($con,$stmt,$data,$announcement_id);
				break;
			case 'update':
				$stmt = oci_parse($con,'BEGIN PKG_ANNOUNCEMENT.UPDATE_ANNOUNCEMENT(:pic,:description,:start_date,:end_date,:thisID);END;');
				oci_bind_by_name($stmt,':pic',$data['pic']);
				oci_bind_by_name($stmt,':description',$data['desc']);
				oci_bind_by_name($stmt,':start_date',$data['start_date']);
				oci_bind_by_name($stmt,':end_date',$data['end_date']);
				oci_bind_by_name($stmt,':thisID',$data['id']);
				oci_execute($stmt,OCI_DEFAULT);
	         	oci_free_statement($stmt);         			
				
				bodyProcedure($con,$stmt,$data,$data['id']);
				break;	
		}	
	}
		
	function bodyProcedure($con,$stmt,$data,$announcement_id){

			$stmt2 = oci_parse($con,'BEGIN PKG_ANNOUNCEMENT.DELETE_DETAIL_ANNOUNCEMENT(:announcement_id);END;');
			oci_bind_by_name($stmt2,':announcement_id',$announcement_id);
		    oci_execute($stmt2,OCI_DEFAULT);
		    oci_free_statement($stmt2);         			
			
         	if(isset($data['chkCorp']) && count($data['chkCorp']) > 0){
         		foreach($data['chkCorp'] as $key => $val){
					$stmt1 = oci_parse($con,'BEGIN PKG_ANNOUNCEMENT.INSERT_DETAIL_ANNOUNCEMENT(:announcement_id,:company_id,:pic);END;');
					oci_bind_by_name($stmt1,':announcement_id',$announcement_id);
					oci_bind_by_name($stmt1,':company_id',$val);
					oci_bind_by_name($stmt1,':pic',$data['pic']);
		         	oci_execute($stmt1,OCI_DEFAULT);
		    		oci_free_statement($stmt1);         			
         		}
         	}
		   
			$r = oci_commit($con);
			// Commit the changes to both tables
			if (!$r) 
			{					
		    	$e = oci_error($con);
				oci_rollback($con);
			}								
	}	
	switch($_POST['action']){
		case 'add' :
			insertData($action,$con,$data,$_POST);
			if($_REQUEST['debug'] == true){				
 				header("Location: dashboard.php?debug=true");				
			}else{
 				header("Location: dashboard.php");				
			}
		break;
		case 'update' :
			insertData($action,$con,$data,$_POST);
			if($_REQUEST['debug'] == true){							
				echo "<pre>";
				print_r($data);
				var_dump($data);
				echo "</pre>";
				
				header("Location: form.php?debug=true&form=update&update=".$_REQUEST['update']);				
			}else{
 				header("Location: dashboard.php");				
			}
			
						
		break;
		default:
			break;
	}
	
   
	function chekedValidation($value,$company){
	  foreach($company as $key => $val){
      if($value == $val){
         return 'checked';
      }	  		  	
	  }	
   	}

?>
<script type="text/javascript" src="<?= 'http://'.JS_PATH;?>form/js_form.js"></script>
<script type="text/javascript">
	function get_employee(id,c_name){
    	$("#superior_name").val(c_name);
    	$("#superior_id").val(id);
	}
	function alwaysChecked(id){
		$('#'+id).attr('checked',true);
		return false;
	}
	$(document).ready(function(){
		//date picker
		$("#txt_start_date").datepicker({
		showOn: "button",
		buttonImage:"<?= 'http://'.IMAGE_PATH?>calendar.gif",
		buttonImageOnly: true
		});

		$("#txt_end_date").datepicker({
			showOn: "button",
			buttonImage:"<?= 'http://'.IMAGE_PATH?>calendar.gif",
			buttonImageOnly: true
			});
	});
</script>

<fieldset class="frm_announcement">
	<legend><?= ucwords($action) ?> Announcement</legend>
<div class='formInput'>	
	<form name='form_announcement' id='form_announcement' action method='post' onsubmit='return submitValidation(this.id)'>
	<input type='hidden' name='action' value='<?= $action ?>'/>
	
	<div>
		<label> Company <span class='red'>*</span></label>
		<input type='checkbox' name='chkCorp[CTI]' id='chk_CTI' style='width:30px;' value='3' checked="checked" onclick="alwaysChecked(this.id)"/> CTI
		<input type='checkbox' name='chkCorp[BPT]' id='chk_BPT' style='width:30px;' value='8' <?= chekedValidation('8',$company);?> /> BPT
		<input type='checkbox' name='chkCorp[CDT]' id='chk_CDT' style='width:30px;' value='7' <?= chekedValidation('7',$company);?> /> CDT
		<input type='checkbox' name='chkCorp[VTI]' id='chk_VTI' style='width:30px;' value='9' <?= chekedValidation('9',$company);?> /> VTI
	</div>

	<div>
	  	<label> Start Date <span class="red">*</span></label> : <input type="text" onblur="return newCekValidation(this.value,this.id);" value="<?= $start_date ?>" id="txt_start_date" name="txt_start_date">
      	<sup class="red error_txt_start_date hidden">* Required</sup>
	</div>
	
	<div>      		
	  	<label> End Date <span class="red">*</span></label> : <input type="text" onblur="return newCekValidation(this.value,this.id);" value="<?= $end_date ?>" id="txt_end_date" name="txt_end_date">
      	<sup class="red error_txt_end_date hidden">* Required</sup>
	</div>
	
	<div>
		<label> Announcement <span class="red">*</span></label> : 
		<textarea rows="5" cols="30" class='form-textarea' name='txt_description' id='txt_description'><?= $desc ?></textarea>
      	<sup class="red error_txt_description hidden">* Required</sup>
	</div>
	
	<div class="mybutton"> <button type="submit" class="button"> Save </button> <button type="reset" class="button"> Reset </button></div>
	</form>		
</div>	
</fieldset>